<?php
class Administrator_UserController extends Nguyen_Controller_Action {
	public function init() {
		$this->loadTemplate ( 'administrator' );
	}
	public function indexAction() {
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$utype = '';
		if($auth->hasIdentity()){
			$utype = $infoUser->type;
		}
		$this->StaffOnly();
		$model = new Administrator_Model_User();
		if ($this->_request->isPost ()) {
			if ($this->_request->getParam ( 'btadd' )) {
				$this->_redirect('/administrator/user/new');
			}
			if ($this->_request->getParam ( 'btedit' )) {
				foreach ($model->GetAllUser() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/user/edit?id='.$value['id']);
				}
			}
			if ($this->_request->getParam ( 'btdelete' )) {
				foreach ($model->GetAllUser() as $value) {
					if($this->_request->getParam($value['id']))
						$model->DelUser($value['id'],$utype);
				}
			}
		}
		$this->view->headTitle ( 'Control Panel | Danh sách thành viên', true );
	}
	public function newAction() {
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$utype = '';
		if($auth->hasIdentity()){
			$utype = $infoUser->type;
		}
		$this->view->utype = $utype;
		$model = new Administrator_Model_User ();
		$this->StaffOnly ();
		if ($this->_request->isPost ()) {
			if ($this->_request->getParam ( 'btsave' )) {
				
				$username = $this->_request->getParam ( 'username', '' );
				$email = $this->_request->getParam ( 'email', '' );
				$password = $this->_request->getParam ( 'password', '' );
				$name = $this->_request->getParam ( 'realname', '' );
				$birthday = $this->_request->getParam ( 'birthday', '' );
				$gender = $this->_request->getParam ( 'gioitinh', '' );
				$address= $this->_request->getParam ( 'address', '' );
				$type = $this->_request->getParam ( 'type', '' );
				
				$email = $this->_request->getParam ( 'email', '' );
				$phone = $this->_request->getParam ( 'phone', '' );
				$mobile = $this->_request->getParam ( 'mobile', '' );
				$status = $this->_request->getParam ( 'multilang', '' );
				
				$f = $_FILES ['image'];
				$file_temp = $f ['tmp_name'];
				$filename = $f ['name'];
				$imgname = md5 ( $model->curentday () ) . $filename;
				if (move_uploaded_file ( $file_temp, PUBLIC_PATH . '/images/user/' . $imgname )) {
				
				} else {
					$imgname = '';
				}
				$model->InsertUserAll($username, md5 ( $password ), $imgname, $name, $birthday, $gender, $address, $email, $type, '1', $phone);
				$this->_redirect ( '/administrator/user' );
			
			}
			if ($this->_request->getParam ( 'btback' )) {
				$this->_redirect ( '/administrator/user' );
			}
			if ($this->_request->getParam ( 'btoption' )) {
				$this->_redirect ( '/administrator' );
			}
		}
		$this->view->headTitle ( 'Control Panel | Thêm thành viên', true );
	}
	
	public function editAction() {
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$utype = '';
		if($auth->hasIdentity()){
			$utype = $infoUser->type;
		}
		$this->view->utype = $utype;
		$this->view->ulogin = $infoUser;
		$model = new Administrator_Model_User ();
		$this->StaffOnly ();
		$id = $this->_request->getParam ( 'id','' );
		$this->view->id = $id;
		if($id == '')$id = $infoUser->id;
		$info = $model->GetUserInfo($id);
		switch ($info['type']) {
			case '1':
			$this->AdminOnly();
			break;
			case '2':
			$this->StaffOnly();
			break;
			case '3':
			$this->ModOnly();
			break;
			default:
				;
			break;
		}
		if ($this->_request->isPost ()) {
			if ($this->_request->getParam ( 'btsave' )) {
				
				$username = $this->_request->getParam ( 'username', '' );
				$email = $this->_request->getParam ( 'email', '' );
				$password = $this->_request->getParam ( 'password', '' );
				$name = $this->_request->getParam ( 'realname', '' );
				$birthday = $this->_request->getParam ( 'birthday', '' );
				$gender = $this->_request->getParam ( 'gioitinh', '' );
				$address= $this->_request->getParam ( 'address', '' );
				$type = $this->_request->getParam ( 'type', '' );
				
				$email = $this->_request->getParam ( 'email', '' );
				$phone = $this->_request->getParam ( 'phone', '' );
				$mobile = $this->_request->getParam ( 'mobile', '' );
				$status = $this->_request->getParam ( 'multilang', '' );
				$f = $_FILES ['image'];
				$file_temp = $f ['tmp_name'];
				$filename = $f ['name'];
				$imgname = md5 ( $model->curentday () ) . $filename;
				if (move_uploaded_file ( $file_temp, PUBLIC_PATH . '/images/user/' . $imgname )) {
				
				} else {
					$imgname = $info['image'];
				}
				($password == null) ? ($model->UpdateUsernoPassAll ( $id,$utype, $username, $imgname, $name, $email, $type , $phone, $birthday, $gender, $address ))
					 : ($model->UpdateUserAll($id, $utype, $username, md5($password), $imgname, $name, $email, $type, $phone, $birthday, $gender, $address));
				$this->_redirect ( '/administrator/user' );
			
			}
			if ($this->_request->getParam ( 'btback' )) {
				$this->_redirect ( '/administrator/user' );
			}
			if ($this->_request->getParam ( 'btoption' )) {
				$this->_redirect ( '/administrator' );
			}
		}
		$this->view->headTitle ( 'Control Panel | Sửa thành viên', true );
	}
	
	public function userinfoAction() {
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		if($auth->hasIdentity()){
			$this->view->uinfo = $infoUser;
		}
		if ($this->_request->isPost ()){
			if($this->_request->getParam('btedit'))
			{
				$this->_redirect ( '/administrator/user/edit' );
			}
		}
		$this->view->headTitle ( 'Control Panel | Thông tin thành viên', true );
	}
	public function changepassAction(){
		
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		if($auth->hasIdentity())
		{	
			
			$id = $infoUser->id;
			if ($this->_request->isPost ()){
				if($this->_request->getParam('btsave'))
				{
					$oldpass = $this->_request->getParam('oldpassword');				
					$model = new Administrator_Model_User();
					$nhan=$model->GetCheckValidPass($id, $oldpass);
					if($nhan >1)
					{
						$newpass = $this->_request->getParam('newpassword');
						$confirmpass = $this->_request->getParam('confirmpass');
						if(($newpass == $confirmpass)&&($newpass))
						{
							 $model->Updatepass($id,md5($newpass) );
							 $this->_redirect ( '/administrator/user' );
						}else {
							echo '<p>Bạn sai mật khẩu mới.Hãy nhập lại</p>';
						}
						
					}
					else {
						echo '<p> Password cũ sai hay bạn chưa nhập mật khẩu</p>';
					}					
				}			
			}	
		}
	}
}